﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Diagnostics;
using System.Xml;


namespace Dashboard
{
    public partial class BackupDashboard : Form
    {
        private string BackupFolder = null;        
        private XmlDocument xReader = null;

        public BackupDashboard()
        {
            InitializeComponent();            
            backupTime.Value = DateTime.Now;
            LoadServiceStatus();
            LoadChecklistBox();
            GenerateBackup bkp = new GenerateBackup();
            bkp.Start();
        }

        private void LoadServiceStatus()
        {
            try
            {
                //if (ServiceController.GetServices().Where(m => m.ServiceName.Equals("DailyBackup")).SingleOrDefault() == null)
                //{
                //    label2.Text = "Not Installed";
                //    btnInstall.Enabled = true;
                //    btnUninstall.Enabled = false;
                //    btnStart.Enabled = false;
                //    btnStop.Enabled = false; 
                //}
                //else
                //{
                //    switch (service.Status)
                //    {
                //        case ServiceControllerStatus.Running:
                //            label2.Text = "Running";
                //            btnInstall.Enabled = false; 
                //            btnUninstall.Enabled=false; 
                //            btnStart.Enabled=false; 
                //            btnStop.Enabled=true; 
                //            break;
                //        case ServiceControllerStatus.Stopped:
                //            label2.Text = "Stopped";
                //            btnInstall.Enabled = false; 
                //            btnUninstall.Enabled=false; 
                //            btnStart.Enabled=true; 
                //            btnStop.Enabled=false; 
                //            break;
                //        case ServiceControllerStatus.Paused:
                //            label2.Text = "Paused"; 
                //            btnInstall.Enabled = false; 
                //            btnUninstall.Enabled=false; 
                //            btnStart.Enabled=false; 
                //            btnStop.Enabled=true; 
                //            break;
                //        case ServiceControllerStatus.StopPending:
                //            label2.Text = "Stopping";
                //            btnInstall.Enabled = false; 
                //            btnUninstall.Enabled=false; 
                //            btnStart.Enabled=true; 
                //            btnStop.Enabled=false; 
                //            break;
                //        case ServiceControllerStatus.StartPending:
                //            label2.Text = "Starting"; 
                //            btnInstall.Enabled = false; 
                //            btnUninstall.Enabled=false; 
                //            btnStart.Enabled=false; 
                //            btnStop.Enabled=true; 
                //            break;
                //        default:
                //            label2.Text = "Status Changing...!"; break;                    
                //    }
                //}
            }
            catch (Exception ex) { }
        }

        private void LoadChecklistBox()
        {
            try
            {
                chkProjectFolders.Items.Clear();
                xReader = new XmlDocument();
                xReader.Load(Path.GetFullPath("Configuration.xml"));
                XmlNodeList folders = xReader.SelectNodes("backup/projectfolder/folder");
                for (int index = 0; index < folders.Count; index++)
                {
                    chkProjectFolders.Items.Add(folders[index].SelectSingleNode("path").InnerText, Convert.ToBoolean(folders[index].SelectSingleNode("active").InnerText));
                }
            }
            catch (Exception ex) { }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Hide();            
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show("Do not forget to save the changes...!", "DailyBackup Service", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (BackupFolderChooser.ShowDialog() == DialogResult.OK)
                {                    
                    BackupFolder = BackupFolderChooser.SelectedPath;                    
                    label1.Text = BackupFolder;
                }
            }
            catch (Exception ex) { }
        }       

        private void btnAddProject_Click(object sender, EventArgs e)
        {
            try
            {
                if (BackupFolderChooser.ShowDialog() == DialogResult.OK)
                {
                    chkProjectFolders.Items.Add(BackupFolderChooser.SelectedPath, true);
                }
            }
            catch (Exception ex) { }
        }

        private void btnUpdateService_Click(object sender, EventArgs e)
        {
            try
            {
                xReader = new XmlDocument();
                xReader.Load(Path.GetFullPath("Configuration.xml"));
                var folderRoot = xReader.SelectSingleNode("backup/projectfolder");
                folderRoot.RemoveAll();                
                for (int index = 0; index < chkProjectFolders.Items.Count; index++)
                {
                    XmlNode folder = xReader.CreateNode(XmlNodeType.Element, "folder", "folder", "");
                    XmlNode path = xReader.CreateNode(XmlNodeType.Element, "path", "path", "");
                    path.InnerText = chkProjectFolders.Items[index].ToString();
                    folder.AppendChild(path);
                    XmlNode active = xReader.CreateNode(XmlNodeType.Element, "active", "active", "");
                    active.InnerText = chkProjectFolders.GetItemChecked(index).ToString();
                    folder.AppendChild(active);
                    folderRoot.AppendChild(folder);
                }
                xReader.Save(Path.GetFullPath("Configuration.xml"));
                MessageBox.Show("Saved...!", "DailyBackup Service", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex) { }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (label1.Text != string.Empty)
                {
                    xReader = new XmlDocument();
                    xReader.Load(Path.GetFullPath("Configuration.xml"));
                    xReader.SelectSingleNode("backup/settings/scheduledtime").InnerText = backupTime.Value.ToString();
                    xReader.SelectSingleNode("backup/settings/backupfolder").InnerText = BackupFolder;
                    xReader.Save(Path.GetFullPath("Configuration.xml"));
                    MessageBox.Show("Saved...!", "DailyBackup Service", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("Please Choose a Backup Folder...!", "DailyBackup Service", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            catch (Exception ex) { }
        }

        private void btnOpenBackup_Click(object sender, EventArgs e)
        {
            try
            {
                if (BackupFolder != null && BackupFolder != "")
                {
                    ProcessStartInfo info = new ProcessStartInfo("explorer.exe");
                    info.UseShellExecute = true;
                    info.Arguments = BackupFolder;
                    Process.Start(info);
                }
                else
                {
                    MessageBox.Show("Please Choose a Backup Folder...!", "DailyBackup Service", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                
            }
            catch (Exception ex) { }
        }
    }
}
